package com.example.a17376080_dlz_finaltest;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import adapter.Comic;
import adapter.World;
import adapter.WorldAdapter;
import okhttp3.Call;
import okhttp3.Response;
import utils.HttpUtil;

public class World_Fragment extends Fragment {
    private Context mContext;
    private static final int UPDATA_TXT=1;
    FloatingActionButton fab;
    WorldAdapter adapter;
    RecyclerView recyclerView;
    TextView tv_world_anthon;
    private List<World> worldList=new ArrayList<>();
    View view;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case UPDATA_TXT:
                    adapter.notifyDataSetChanged();

                    break;

            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.world_fragment,null);
        //发布信息
        addMessage();
        initView();
        mContext=getContext();
        recyclerView=view.findViewById(R.id.recycle_world_view);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
       adapter=new WorldAdapter(worldList,mContext);
       recyclerView.setAdapter(adapter);
        if(worldList==null||worldList.isEmpty()){
            startOkHttp();
        }
        return view;
    }

    private void startOkHttp() {
        HttpUtil.sendOkHttpRequest("http://106.14.221.71:8080/Service_dlz/release_findAll.action",new okhttp3.Callback(){
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData=response.body().string();
                Log.d("回来的数据：", responseData);
                //GSON解析
                parseJSONWithGSON(responseData);
                Message message=new Message();
                message.what=UPDATA_TXT;
                handler.sendMessage(message);

            }

            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("没有得到信息", "onFailure: ");
            }
        });
    }
    private void parseJSONWithGSON(String jsonData) {
        Gson gson=new Gson();
        worldList.addAll((Collection<? extends World>) gson.fromJson(jsonData,new TypeToken<List<World>>(){}.getType()));


    }

    private void addMessage() {
        fab=view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp=getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
                String name=sp.getString("currentUsername","");
                String pswd=sp.getString("currentPassword","");
                if (!name.equals("")&&!pswd.equals("")){
                    Intent intent=new Intent(getActivity(),ReleaseActivity.class);
                    startActivity(intent);
                }else {
                    Intent intent=new Intent(getActivity(),EnterActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

    private void initView() {
        tv_world_anthon=view.findViewById(R.id.tv_world_anthon);

    }
    private String getRundomLengthName(String name){
        Random random=new Random();
        int length=random.nextInt(20)+1;
        StringBuilder builder=new StringBuilder();
        for (int i=0;i<length;i++){
            builder.append(name);
        }
        return builder.toString();
    }
}

